/* -*- mode: c; tab-width: 2; c-tab-always-indent: t; c-basic-offset: 2; comment-column: 0 -*- */
var wwwos = {
  templates: {},
  utils: {}
}

wwwos.OS = new Class({
  options: {
    absolute_url: '',
    theme: 'default',
    js_registries: ['window.js', 'templates.js']
  },
  
  zIndex:         10,
  counter:        0,
  windows:        {},
  taskbar:        null,
  activeWindowId:   null,
  
  initialize: function(options) {
    this.options = $merge(this.options, options);
    this.setTheme(this.options.theme);
    this.initJavascript();
    
    document.addEvent('jsready', function() {
      this.initTaskbar();
    }.bind(this));
    
  },
  
  initTaskbar: function() {
    var div = new Element('div', {'html': wwwos.templates.taskbar});
    this.taskbar = div.firstChild;
    document.body.insertBefore(div.firstChild, document.body.firstChild);
  },
  
  initJavascript: function(callback) {
    var head = document.getElement('head');
    this.options.js_registries.each(function(js_filename, index) {
      var url = this.options.absolute_url + 'js/' + js_filename;
      if (index==this.options.js_registries.length-1)
        new Asset.javascript(url, {
          'onload': function() {
            document.fireEvent('jsready', [document]);
          }.bind(this)
        });
      else
        new Asset.javascript(url);
    }.bind(this));
  },
  
  setTheme: function(theme) {
    var head = document.getElement('head');
    if (!head.getElementById(theme+'_css')) {
      var url = this.options.absolute_url + 'themes/' + theme + '/css/main.css';
      new Asset.css(url, {id: theme+'_css',});
    }
    document.getElement('body').className = theme;
  },
  
  getTheme: function() {
    return this.options.theme;
  },
  
  register_window: function(opts) {
    var newId = 'window_'+this.counter;
    var win = new wwwos.Window(opts);
    this.windows[newId] = win;
    this.register_taskbar(newId);
    this.attachWindowEvents(newId);
    this.activeWindowId = newId;
    this.updateAllWindowsStatus(newId);    
    this.counter++;
    document.body.insertBefore(win.el, document.body.firstChild);
  },
  
  register_taskbar: function(id) {
    var options = this.windows[id].options;
    var opts = {'id': id, 'icon': options.icon, 'title': options.header}
    var div = new Element('div', {'html': wwwos.templates.taskbar_item.bind(opts)});
    this.attachTaskbarItemEvents(div.firstChild);
    this.taskbar.getElement('ul').appendChild(div.firstChild);
    
  },
  
  attachTaskbarItemEvents: function(item) {
    var id = item.id;
    item.getElement('a').addEvent('click', function(e){
      e.stop();
      var activeWindow = this.windows[id];
      if (activeWindow.enable)
        activeWindow.toogle();
      else {
        if (activeWindow.status=='minimum') activeWindow.normalize();
        this.updateAllWindowsStatus(id);
      }
    }.bind(this));
  },
  
  attachWindowEvents: function(id) {
    var win = this.windows[id];    
    win.el.addEvent('mousedown', function(e) {
      if (this.activeWindowId && this.activeWindowId!=id) {
        this.updateAllWindowsStatus(id);
      }
    }.bind(this));
  },
  
  updateAllWindowsStatus: function(id) {
    for (var i in this.windows) {
      if (i==id) {
        this.windows[id].setEnable();
        this.windows[id].setZIndex(this.zIndex+1);
        this.activeWindowId = id;
        this.taskbar.getElementById(id).addClass('enable');
      } else {
        this.windows[i].setDisable();
        this.windows[i].setZIndex(this.zIndex);
        this.taskbar.getElementById(i).removeClass('enable');
      }
    }
  }
});

String.implement({
  bind: function(dict, rx) {
    if (!rx)
      rx = /{{([^}]+)}}/g;
    return this.replace(rx, function(m, k) {
      return (k in dict) ? dict[k] : '';
    });
  }
});
